import requests
from bs4 import BeautifulSoup
import time
import random
import os

data_path = r'D:\图灵程序设计丛书'

def get_books_link():
    url = "https://github.com/ChenyangGao/ituring_books/tree/master/%E5%9B%BE%E7%81%B5%E7%A8%8B%E5%BA%8F%E8%AE%BE%E8%AE%A1%E4%B8%9B%E4%B9%A6"
    r = requests.get(url)

    soup = BeautifulSoup(r.text, "html.parser")
    books = []
    for item in soup.find_all("a", attrs={'class': "js-navigation-open link-gray-dark"}):
        temp = {
            "link": "https://cdn.jsdelivr.net/gh" + item['href'].replace("/blob/", "@"),
            'title': item['title']
        }
        books.append(temp)
    return books

books_info = get_books_link()

for item in books_info:
    print(item['title'])
    r = requests.get(item['link'])
    with open(os.path.join(data_path,item['title']), "wb") as code:
        code.write(r.content)
    time.sleep(random.randint(0,9))